using Volo.Abp.Authorization.Permissions;
using Volo.Abp.Localization;

namespace Matrix.ServiceDiscovery.Permissions
{
    /// <summary>
    /// 服务发现权限定义
    /// </summary>
    public static class ServiceDiscoveryPermissions
    {
        public const string GroupName = "ServiceDiscovery";

        // 服务发现基础权限
        public static class ServiceDiscovery
        {
            public const string Default = GroupName + ".ServiceDiscovery";
            public const string Create = GroupName + ".ServiceDiscovery.Create";
            public const string Edit = GroupName + ".ServiceDiscovery.Edit";
            public const string Delete = GroupName + ".ServiceDiscovery.Delete";
            public const string Manage = GroupName + ".ServiceDiscovery.Manage";
            public const string View = GroupName + ".ServiceDiscovery.View";
            public const string Statistics = GroupName + ".ServiceDiscovery.Statistics";
        }

        // 服务实例权限
        public static class ServiceInstance
        {
            public const string Default = GroupName + ".ServiceInstance";
            public const string Create = GroupName + ".ServiceInstance.Create";
            public const string Edit = GroupName + ".ServiceInstance.Edit";
            public const string Delete = GroupName + ".ServiceInstance.Delete";
            public const string Manage = GroupName + ".ServiceInstance.Manage";
            public const string View = GroupName + ".ServiceInstance.View";
            public const string Enable = GroupName + ".ServiceInstance.Enable";
            public const string Disable = GroupName + ".ServiceInstance.Disable";
        }

        // 负载均衡权限
        public static class LoadBalancing
        {
            public const string Default = GroupName + ".LoadBalancing";
            public const string Configure = GroupName + ".LoadBalancing.Configure";
            public const string View = GroupName + ".LoadBalancing.View";
        }

        // 健康检查权限
        public static class HealthCheck
        {
            public const string Default = GroupName + ".HealthCheck";
            public const string View = GroupName + ".HealthCheck.View";
            public const string Manage = GroupName + ".HealthCheck.Manage";
        }

        // 系统管理权限
        public static class System
        {
            public const string Default = GroupName + ".System";
            public const string Configuration = GroupName + ".System.Configuration";
            public const string Monitoring = GroupName + ".System.Monitoring";
            public const string CacheManagement = GroupName + ".System.CacheManagement";
        }

        /// <summary>
        /// 获取所有权限定义
        /// </summary>
        public static string[] GetAll()
        {
            return new[]
            {
                // 服务发现权限
                ServiceDiscovery.Default,
                ServiceDiscovery.Create,
                ServiceDiscovery.Edit,
                ServiceDiscovery.Delete,
                ServiceDiscovery.Manage,
                ServiceDiscovery.View,
                ServiceDiscovery.Statistics,

                // 服务实例权限
                ServiceInstance.Default,
                ServiceInstance.Create,
                ServiceInstance.Edit,
                ServiceInstance.Delete,
                ServiceInstance.Manage,
                ServiceInstance.View,
                ServiceInstance.Enable,
                ServiceInstance.Disable,

                // 负载均衡权限
                LoadBalancing.Default,
                LoadBalancing.Configure,
                LoadBalancing.View,

                // 健康检查权限
                HealthCheck.Default,
                HealthCheck.View,
                HealthCheck.Manage,

                // 系统管理权限
                System.Default,
                System.Configuration,
                System.Monitoring,
                System.CacheManagement
            };
        }
    }

    /// <summary>
    /// 服务发现权限定义提供者
    /// </summary>
    public class ServiceDiscoveryPermissionDefinitionProvider : PermissionDefinitionProvider
    {
        public override void Define(IPermissionDefinitionContext context)
        {
            var serviceDiscoveryGroup = context.AddGroup(ServiceDiscoveryPermissions.GroupName, L("ServiceDiscovery"));

            // 服务发现基础权限
            var serviceDiscoveryPermission = serviceDiscoveryGroup.AddPermission(
                ServiceDiscoveryPermissions.ServiceDiscovery.Default,
                L("Permission:ServiceDiscovery"));

            serviceDiscoveryPermission.AddChild(
                ServiceDiscoveryPermissions.ServiceDiscovery.Create,
                L("Permission:ServiceDiscovery.Create"));

            serviceDiscoveryPermission.AddChild(
                ServiceDiscoveryPermissions.ServiceDiscovery.Edit,
                L("Permission:ServiceDiscovery.Edit"));

            serviceDiscoveryPermission.AddChild(
                ServiceDiscoveryPermissions.ServiceDiscovery.Delete,
                L("Permission:ServiceDiscovery.Delete"));

            serviceDiscoveryPermission.AddChild(
                ServiceDiscoveryPermissions.ServiceDiscovery.Manage,
                L("Permission:ServiceDiscovery.Manage"));

            serviceDiscoveryPermission.AddChild(
                ServiceDiscoveryPermissions.ServiceDiscovery.View,
                L("Permission:ServiceDiscovery.View"));

            serviceDiscoveryPermission.AddChild(
                ServiceDiscoveryPermissions.ServiceDiscovery.Statistics,
                L("Permission:ServiceDiscovery.Statistics"));

            // 服务实例权限
            var serviceInstancePermission = serviceDiscoveryGroup.AddPermission(
                ServiceDiscoveryPermissions.ServiceInstance.Default,
                L("Permission:ServiceInstance"));

            serviceInstancePermission.AddChild(
                ServiceDiscoveryPermissions.ServiceInstance.Create,
                L("Permission:ServiceInstance.Create"));

            serviceInstancePermission.AddChild(
                ServiceDiscoveryPermissions.ServiceInstance.Edit,
                L("Permission:ServiceInstance.Edit"));

            serviceInstancePermission.AddChild(
                ServiceDiscoveryPermissions.ServiceInstance.Delete,
                L("Permission:ServiceInstance.Delete"));

            serviceInstancePermission.AddChild(
                ServiceDiscoveryPermissions.ServiceInstance.Manage,
                L("Permission:ServiceInstance.Manage"));

            serviceInstancePermission.AddChild(
                ServiceDiscoveryPermissions.ServiceInstance.View,
                L("Permission:ServiceInstance.View"));

            serviceInstancePermission.AddChild(
                ServiceDiscoveryPermissions.ServiceInstance.Enable,
                L("Permission:ServiceInstance.Enable"));

            serviceInstancePermission.AddChild(
                ServiceDiscoveryPermissions.ServiceInstance.Disable,
                L("Permission:ServiceInstance.Disable"));

            // 负载均衡权限
            var loadBalancingPermission = serviceDiscoveryGroup.AddPermission(
                ServiceDiscoveryPermissions.LoadBalancing.Default,
                L("Permission:LoadBalancing"));

            loadBalancingPermission.AddChild(
                ServiceDiscoveryPermissions.LoadBalancing.Configure,
                L("Permission:LoadBalancing.Configure"));

            loadBalancingPermission.AddChild(
                ServiceDiscoveryPermissions.LoadBalancing.View,
                L("Permission:LoadBalancing.View"));

            // 健康检查权限
            var healthCheckPermission = serviceDiscoveryGroup.AddPermission(
                ServiceDiscoveryPermissions.HealthCheck.Default,
                L("Permission:HealthCheck"));

            healthCheckPermission.AddChild(
                ServiceDiscoveryPermissions.HealthCheck.View,
                L("Permission:HealthCheck.View"));

            healthCheckPermission.AddChild(
                ServiceDiscoveryPermissions.HealthCheck.Manage,
                L("Permission:HealthCheck.Manage"));

            // 系统管理权限
            var systemPermission = serviceDiscoveryGroup.AddPermission(
                ServiceDiscoveryPermissions.System.Default,
                L("Permission:System"));

            systemPermission.AddChild(
                ServiceDiscoveryPermissions.System.Configuration,
                L("Permission:System.Configuration"));

            systemPermission.AddChild(
                ServiceDiscoveryPermissions.System.Monitoring,
                L("Permission:System.Monitoring"));

            systemPermission.AddChild(
                ServiceDiscoveryPermissions.System.CacheManagement,
                L("Permission:System.CacheManagement"));
        }

        private static LocalizableString L(string name)
        {
            return LocalizableString.Create<MatrixServiceDiscoveryResource>(name);
        }
    }
}